package cn.edu360.hadoop.factory;

import cn.edu360.hadoop.util.HdfsFileSystemUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;

public class HdfsFileSystem implements HdfsFileSystemFactory {


    public void upload(String src, String dest) {

        FileSystem cli = HdfsFileSystemUtil.getCli(new Configuration());

        try {
            cli.copyFromLocalFile(new Path(src),new Path(dest));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            HdfsFileSystemUtil.close(cli);
        }


    }

    public boolean mkDir(String fileName) {
    	
        FileSystem cli = HdfsFileSystemUtil.getCli(new Configuration());

        try {
            return cli.mkdirs(new Path(fileName));

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            HdfsFileSystemUtil.close(cli);
        }

        return Boolean.FALSE  ;
    }


    public boolean ifExist(String filePath) {
    	
        FileSystem cli = HdfsFileSystemUtil.getCli(new Configuration());

        boolean exists = Boolean.FALSE;
        try {
            exists = cli.exists(new Path(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            HdfsFileSystemUtil.close(cli);
        }
        return exists;
    }
}
